package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public Map<String, Object> getItemInfo(Long albumId) {

        // 1.创建响应结果 Map 对象 HashMap 在多线程环境下并发读写并不安全 —— 导致 key 覆盖 导致死循环
        // 采用线程安全的 : ConcurrentHashMap
        Map<String, Object> mapResult = new ConcurrentHashMap<>();

        // 2.远程调用专辑服务基本信息—— 封装 albumInfo 属性
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(
                ()->{
                    AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
                    Assert.notNull(albumInfo,"专辑：{}不存在",albumId);
                    mapResult.put("albumInfo",albumInfo);
                    return albumInfo;
                },threadPoolExecutor
        );

        // 3.远程调用专辑服务获取专辑统计信息 —— 封装albumStatVo属性
        CompletableFuture<Void> albumStatCompletableFuture = CompletableFuture.runAsync(
                ()->{
                    AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
                    Assert.notNull(albumStatVo,"专辑统计信息：{}不存在",albumId);
                    mapResult.put("albumStatVo",albumStatVo);
                },threadPoolExecutor
        );

        // 4.远程调用专辑服务获取专辑分类信息 —— 封装baseCategoryView属性
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(
                albumInfo -> {
                    BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
                    Assert.notNull(baseCategoryView,"分类：{}不存在",albumInfo.getCategory3Id());
                    mapResult.put("baseCategoryView",baseCategoryView);
                },threadPoolExecutor
        );

        // 5.远程调用用户服务获取主播信息 —— 封装announcer 属性
        CompletableFuture<Void> announcerCompletableFuture  = albumInfoCompletableFuture.thenAcceptAsync(
                albumInfo -> {
                    UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
                    Assert.notNull(userInfoVo,"用户：{}不存在",albumInfo.getUserId());
                    mapResult.put("announcer",userInfoVo);
                },threadPoolExecutor
        );

        // 6.组合异步任务，阻塞等待所有异步任务执行完毕
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                albumStatCompletableFuture,
                baseCategoryViewCompletableFuture,
                announcerCompletableFuture

        ).join();

        return mapResult;

    }
}
